package com.libre.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.libre.common.base.BaseController;
import com.libre.common.enums.ResultEnum;
import com.libre.common.utils.PageUtil;
import com.libre.common.utils.ResultUtil;
import com.libre.common.vo.PageVo;
import com.libre.common.vo.Result;
import com.libre.system.entity.Dict;
import com.libre.system.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @description   字典
* @author        庞留杰
* @date          2021/12/20 22:59
* @version       V1.0
**/
@Slf4j
@RequestMapping("/dict")
@RestController
public class DictController  extends BaseController {

    @Autowired
    private DictService service;

    //查询带分页列表
    @GetMapping("/findPage")
    public Result<IPage<Dict>> findPage(@ModelAttribute PageVo pageVo, @ModelAttribute Dict dict) {
        IPage<Dict> page = service.findPage(PageUtil.initMpPage(pageVo),dict);
        return new ResultUtil<IPage<Dict>>().setData(page);
    }

    //查询不带分页列表
    @GetMapping("/findList")
    public Result<List<Dict>> findList(@ModelAttribute Dict dict) {
        List<Dict> list = service.findList(dict);
        return new ResultUtil<List<Dict>>().setData(list);
    }

    //通过ID查询
    @GetMapping("/findById")
    public Result<Dict> findById(String id) {
        Dict dict = service.findById(id);
        return new ResultUtil<Dict>().setData(dict);
    }

    //添加
    @PostMapping("/save")
    public Result save(@RequestBody Dict dict) {
        boolean boo = service.add(dict);
        if(boo){
            return new ResultUtil().setSuccessMsg(ResultEnum.OK.getReasonPhraseCN());
        }else {
            return new ResultUtil().setErrorMsg(ResultEnum.FAIL.getReasonPhraseCN());
        }
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Dict dict) {
        boolean boo = service.update(dict);
        if(boo){
            return new ResultUtil().setSuccessMsg(ResultEnum.OK.getReasonPhraseCN());
        }else {
            return new ResultUtil().setErrorMsg(ResultEnum.FAIL.getReasonPhraseCN());
        }
    }

    //删除
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        boolean boo = service.delete(id);
        if(boo){
            return new ResultUtil().setSuccessMsg(ResultEnum.OK.getReasonPhraseCN());
        }else {
            return new ResultUtil().setErrorMsg(ResultEnum.FAIL.getReasonPhraseCN());
        }
    }

}
